﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

using SharpSvn;

using SubManager.Entity;
using SubManager.Logic;
using SubManager.Interfaces;
using SubManager.DAL;
using Etier.IconHelper;


namespace SubManager.Win
{
    public partial class MainForm : Form
    {
        #region Contructure
        public MainForm()
        {
            InitializeComponent();

            _reposExplorer.ItemSelected += new TreeViewEventHandler(
                repositoryExplorer_ItemSelected);
        }

        void repositoryExplorer_ItemSelected(object sender, TreeViewEventArgs e)
        {
            
        }
        #endregion

        #region Fields
        private string _connectedServer = string.Empty;
        private List<string> _repositories = new List<string>();
        #endregion

        #region Methods

        /// <summary>
        /// get data from data row, and create a new user entity.
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private static UserEntity RowToUserEntity(DataGridViewRow row)
        {
            string id = (row.Cells[0].Value != null) ? row.Cells[0].Value.ToString() : null;
            string name = (row.Cells[1].Value != null) ? row.Cells[1].Value.ToString() : null;
            string pwd = (row.Cells[2].Value != null) ? row.Cells[2].Value.ToString() : null;
            string email = (row.Cells[3].Value != null) ? row.Cells[3].Value.ToString() : null;

            UserEntity entity = new UserEntity();
            entity.Id = id;
            entity.Name = name;
            entity.Email = email;
            entity.Password = pwd;
            return entity;
        }
        #endregion

        #region Event Handlers

        private void OnNewRepos(object sender, EventArgs e)
        {

        }

        private void OnSaveRepos(object sender, EventArgs e)
        {

        }

        private void OnRemoveRepos(object sender, EventArgs e)
        {

        }

        private void _cmbUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter != e.KeyCode)
                return;
        }

        private void _btnCreateRepos_Click(object sender, EventArgs e)
        {
            SvnRepositoryClient src = new SvnRepositoryClient();
            try
            {
                bool success = src.CreateRepository(@"test1");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void _btnRevision_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // close database.
            Db4oHelper<UserEntity>.Close();
        }

        private void _btnLoadUsers_Click(object sender, EventArgs e)
        {
            IList<UserEntity> users = Db4oHelper<UserEntity>.QueryAll(typeof(UserEntity));

            this.dataGridView1.DataSource = users;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void _btnNewUser_Click(object sender, EventArgs e)
        {
            PopupForm form = new PopupForm(new UserUC());
            if (DialogResult.OK == form.ShowDialog())
            {
                this.dataGridView1.DataSource = Db4oHelper<UserEntity>.QueryAll(typeof(UserEntity));
            }
        }

        private void _btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                IUser logic = new User();
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    UserEntity entity = RowToUserEntity(row);

                    List<UserEntity> found = logic.Query(entity);
                    foreach(UserEntity ue in found)
                        logic.Delete(ue);
                }
                this.dataGridView1.DataSource = Db4oHelper<UserEntity>.QueryAll(typeof(UserEntity));
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

            UserEntity entity = RowToUserEntity(row);

            PopupForm form = new PopupForm(new UserUC(entity));
            form.ShowDialog();
        }

        private void _btnModifyUser_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                UserEntity entity = RowToUserEntity(dataGridView1.SelectedRows[0]);

                PopupForm form = new PopupForm(new UserUC(entity));

                form.ShowDialog();
            }
        }

        #endregion

    }
}